//
//  WaterFlowLayout.m
//  HYHOME
//
//  Created by Mac on 16/1/27.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "WaterFlowLayout.h"


@interface WaterFlowLayout()
// 获取item的总数量
@property (nonatomic,assign) NSUInteger numberOfItems;

// 用来保存每一列的高度
@property (nonatomic,retain) NSMutableArray *columnHeights;

@property (nonatomic,retain) NSMutableArray *arrForAttributes; // 保存 Item 属性的数组
@property (nonatomic,assign) CGFloat xForItemOrigin; //item 的x值
@property (nonatomic,assign) CGFloat yForItemOrigin; //item 的y值
@property (nonatomic,assign) NSInteger shortIndex;

@end

@implementation WaterFlowLayout

- (void)prepareLayout{

    self.columnHeights = @[@(10),@(10)].mutableCopy;
    
    self.arrForAttributes = [NSMutableArray array];
    
    // 获取 Item 数量
    self.numberOfItems = [self.collectionView numberOfItemsInSection:0 ];

    for (int i = 0 ; i<self.numberOfItems; i++) {
        
        
        [self getItemOriginPoint];
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i  inSection:0];
        
        [self setFrame:indexPath];
      
        
    }
    

}


- (void)getItemOriginPoint{
    NSInteger shortIndex = 0;
    CGFloat shortHeight = MAXFLOAT;
    
    for (int i = 0; i<2; i++) {
        CGFloat currentHeight = [self.columnHeights[i]floatValue];
        
        if (currentHeight < shortHeight) {
            shortHeight = currentHeight;
            shortIndex = i;
        }
    }
    
    self.shortIndex = shortIndex;
    self.xForItemOrigin = 10 + (self.itemSize.width + self.insertItemSpacing)*shortIndex;
    self.yForItemOrigin = shortHeight + self.insertItemSpacing;
    

}

- (void) setFrame:(NSIndexPath *)indexPath{

    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGFloat itemHeight = 0;
    if (_delegate && [_delegate respondsToSelector:@selector(heightForItemIndexPath:)]) {
        
        itemHeight = [_delegate heightForItemIndexPath:indexPath];
    }
    
    layoutAttributes.frame = CGRectMake(self.xForItemOrigin, self.yForItemOrigin, self.itemSize.width, itemHeight);
    
    [self.arrForAttributes addObject:layoutAttributes];
    
    self.columnHeights[_shortIndex] = @(self.yForItemOrigin + itemHeight);
}


// 获取最长列的索引
- (NSInteger)p_indexForLongColum{
    
    NSInteger longIndex = 0;
    CGFloat longHeight = 0;
    
    for (int i = 0; i < 2; i++) {
        
        CGFloat currentHeight = [self.columnHeights[i] floatValue];
        if (currentHeight > longHeight) {
            longHeight = currentHeight;
            longIndex = i;
        }
    }
    return longIndex;
}




// 计算contentView 大小
- (CGSize)collectionViewContentSize{
    
    NSInteger longIndex = [self p_indexForLongColum];
    
    CGFloat longHeight = [self.columnHeights[longIndex] floatValue];
    CGSize contentSize = self.collectionView.frame.size;
    contentSize.height = longHeight + 10;
    
    return contentSize;
}


// 返回每一个item 的attribute
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    
    return self.arrForAttributes;
}




@end